package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyMiaoDai;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 徐州秒代＆安心
 *
 * @author WJB
 */
@ApiPlan(productId = "336", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class ApiOrgPhoneMaskMiaoDai implements ApiOrgPhoneMaskConvertHandler {
    private static final String IV = "4e1d468d1bbabb4b";
    private static final String KEY = "hbt4j8ql7n7yjtfb";
    private static final String KEY_APPLY = "c1977cdb4e1d468d1bbabb4b7891b5ad";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        Map<Object, Object> phone = new LinkedHashMap<>();
        phone.put("phone", apiDataExchangeParameter.getPhoneMask());

        Map<Object, Object> newMap = new LinkedHashMap<>();
        newMap.put("businessCode", "P1");

        AES aes = AES.createEcb(KEY.getBytes());
        String s = aes.encryptToBase64Str(JsonUtils.toJson(phone));
        newMap.put("data", s);
        return newMap;
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        JSONArray data = jsonObject.getJSONArray("data");
        if (ValidParam.isEmpty(data)) {
            return RespOrgPhoneMaskDataExchange.ofSuc();
        }
        RespOrgPhoneMaskDataExchange respOrgPhoneMaskDataExchange = RespOrgPhoneMaskDataExchange.ofSuc();
        List<String> md5 = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            // 获取数组中的每个元素
            String phoneMd5 = data.getString(i);
            md5.add(phoneMd5);
        }
        respOrgPhoneMaskDataExchange.setListPhoneMd5(md5);
        return respOrgPhoneMaskDataExchange;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        // 客户姓名
        OrgApplyMiaoDai data = new OrgApplyMiaoDai();
        data.setCustName(apiApplyParameter.getUserName());
        data.setPhoneNo(apiApplyParameter.getPhone());
        // 年龄(如：39)
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        data.setAge(apiFieldConverter.getAgeInt());
        // 性别
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 0)
                .when(apiApplyParameter.getSex(), "1", 1)
                .end(0);
        data.setSex(sex);
        // 城市
        data.setCity(apiApplyParameter.getCity());
        // 代发
        Integer salaryPayMethod = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "2", 0)
                .when(apiApplyParameter.getSalaryPayMethod(), "3", 0)
                .end(0);
        data.setSalaryType(salaryPayMethod);
        // 需求资金
        Integer loanAmount = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 1)
                .when(apiApplyParameter.getLoanAmount(), "2", 3)
                .when(apiApplyParameter.getLoanAmount(), "3", 4)
                .when(apiApplyParameter.getLoanAmount(), "4", 5)
                .end(1);
        data.setLoanAmount(loanAmount);
        // 职业
        Integer career = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", 2)
                .when(apiApplyParameter.getCareer(), "2", 0)
                .when(apiApplyParameter.getCareer(), "3", 1)
                .when(apiApplyParameter.getCareer(), "4", 3)
                .end(1);
        data.setProfession(career);
        // 公积金
        Integer providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 0)
                .when(apiApplyParameter.getProvidentFund(), "2", 2)
                .when(apiApplyParameter.getProvidentFund(), "3", 1)
                .when(apiApplyParameter.getProvidentFund(), "4", 2)
                .end(0);
        data.setProvidentFund(providentFund);
        // 社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 0)
                .when(apiApplyParameter.getSocialSecurity(), "2", 1)
                .when(apiApplyParameter.getSocialSecurity(), "3", 1)
                .when(apiApplyParameter.getSocialSecurity(), "4", 1)
                .end(0);
        data.setSocialSecurity(socialSecurity);
        data.setCompany_id(26);
        data.setPromotion_prefix("推广9");
        // 车
        Integer CarInfo = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 0)
                .when(apiApplyParameter.getCarInfo(), "2", 1)
                .when(apiApplyParameter.getCarInfo(), "3", 1)
                .when(apiApplyParameter.getCarInfo(), "4", 1)
                .end(0);
        data.setCar(CarInfo);
        // 房
        Integer RealEstateInfo = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 1)
                .end(0);
        data.setEstate(RealEstateInfo);
        // 保单
        Integer insurancePolicy = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 0)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 1)
                .end(0);
        data.setLifeInsurance(insurancePolicy);
        // 信用卡
        Integer creditLimit = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", 0)
                .when(apiApplyParameter.getCreditLimit(), "2", 1)
                .when(apiApplyParameter.getCreditLimit(), "3", 2)
                .when(apiApplyParameter.getCreditLimit(), "4", 3)
                .when(apiApplyParameter.getCreditLimit(), "5", 4)
                .when(apiApplyParameter.getCreditLimit(), "6", 1)
                .end(0);
        data.setCreditCardLimit(creditLimit);
        data.setMicrocredit(2);
        // 营业执照
        Integer value = ChooseEq.create()
                .when(apiApplyParameter.getBizLicenseYear(), "1", 0)
                .when(apiApplyParameter.getBizLicenseYear(), "2", 1)
                .when(apiApplyParameter.getBizLicenseYear(), "3", 1)
                .when(apiApplyParameter.getBizLicenseYear(), "4", 1)
                .when(apiApplyParameter.getBizLicenseYear(), "5", 1)
                .end(0);
        data.setBusiness_license(value);
        data.setContent(apiFieldConverter.getExtendInfo());

        Map<Object, Object> newMap = new LinkedHashMap<>();
        newMap.put("businessCode", "P1");

        AES aes = AES.createCbc(KEY_APPLY.getBytes(), IV.getBytes());
        String s = aes.encryptToBase64Str(JsonUtils.toJson(data));
        newMap.put("data", s);
        return newMap;
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getString("dealStatus"), "0000")) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
